﻿

#include <string>
#include <string.h>
#include <vector>

#include "base/Types/SPtr.h"


#ifndef __VIDEOREADER_H__
#define __VIDEORWADER_H__


namespace pi {


class VideoFrame
{
public:
    VideoFrame():img(nullptr),height(0),width(0),channel(0) {}

    VideoFrame(const VideoFrame &ref)
    {
        height = ref.height;
        width = ref.width;
        channel = ref.channel;

        if(ref.img!=nullptr){
            size_t size_ = height * width * channel * sizeof(u_char);
            img = new u_char[size_];
            memcpy(img,ref.img,size_);
        }
    }

    virtual ~VideoFrame(){
        release();
    }

    virtual std::string type() { return "Default VideoFrame"; }

    int release(){
        if(img!=nullptr){
            delete img;
            img = nullptr;
        }
    }

    virtual int call(const std::string& command, void* arg = NULL)
    { return -1; }

public:
    VideoFrame& operator =(const VideoFrame &ref)
    {
        height = ref.height;
        width = ref.width;
        channel = ref.channel;

        if(ref.img!=nullptr){
            size_t size_ = height * width * channel * sizeof(u_char);
            img = new u_char[size_];
            memcpy(img,ref.img,size_);
        }
        return *this;
    }

public:
    void*       img;                        ///< image
    int         height,width,channel;
    double      timestamp;                  ///< timestamp (seconds since 1970/1/1, UTC time)
};

class VideoReaderImpl;
class VideoReader
{
public:
    VideoReader();
    virtual ~VideoReader();

    /*
        Video device:
        OpenCV: 1
        OpenCV: /home/test.mp4
        GigE: 192.168.0.10
    */
    virtual int open(const std::string& name);
    virtual int close(void);

    virtual std::string type();
    virtual int call(const std::string& command, void* arg = NULL);

    virtual int grabImage(VideoFrame& videoFrame);

    /*
        optNames:
            ImgWidth        - image width
            ImgHeight       - image height
            FPS             - frame per second
            ColorMode       - color mode
                                MONO8, RGB24, BGR24, YUV444, ....

            Exposure        - Exposure
                .shuttur    - shuttur speed (us)
            Gain            - Gain (db)
    */
    virtual int getOpt(const std::string& optName, std::string& opt);
    virtual int setOpt(const std::string& optName, std::string opt);

protected:
    SPtr<VideoReaderImpl>     impl;
};

} // end of namespace pi

typedef SPtr<pi::VideoReader> (*getVideoReaderFunction)(void);

extern "C"
{
    SPtr<pi::VideoReader> getVideoReader();
}
#endif

